package top.drinyeit.weilong.viewmodel;

import android.content.Intent;
import android.view.View;
import android.widget.Toast;

import com.scu.miomin.shswiperefresh.core.SHSwipeRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import top.drinyeit.weilong.BR;
import top.drinyeit.weilong.R;
import top.drinyeit.weilong.activity.ChildOrderActivity;
import top.drinyeit.weilong.app.Constant;
import top.drinyeit.weilong.common.CommonAdapter;
import top.drinyeit.weilong.common.ItemClick;
import top.drinyeit.weilong.fragment.OrderFragment;
import top.drinyeit.weilong.model.OrderFatherListBean;
import top.drinyeit.weilong.retrofit.HttpResponse;
import top.drinyeit.weilong.retrofit.RetrofitMethod;
import top.drinyeit.weilong.retrofit.subscriber.CommonSubscriber;
import top.drinyeit.weilong.retrofit.subscriber.SubscriberListener;
import top.drinyeit.weilong.util.SPUtil;

import static top.drinyeit.weilong.app.Constant.COMMON_SUCCESS;

/**
 * Created by YJY on 2018/1/5.
 */

public class OrderVM {

    private OrderFragment fragment;
    private CommonAdapter<OrderFatherListBean.FatherOrderBean> adapter;
    private int type;   //订单状态：0未支付 1待配送，2待签收，3完成 4关闭 -1全部
    private int pageIndex;

    public OrderVM(OrderFragment fragment, int type) {
        this.fragment = fragment;
        this.type = type;
        initRefreshLayout();
        getOrderList(1);
    }

    private void initRefreshLayout() {
        fragment.shSwipeRefreshLayout.setLoadmoreEnable(false);
        fragment.shSwipeRefreshLayout.setOnRefreshListener(new SHSwipeRefreshLayout.SHSOnRefreshListener() {
            @Override
            public void onRefresh() {
                getOrderList(1);
            }

            @Override
            public void onLoading() {
                getOrderList(pageIndex + 1);
            }

            @Override
            public void onRefreshPulStateChange(float v, int i) {

            }

            @Override
            public void onLoadmorePullStateChange(float v, int i) {

            }
        });
    }

    private void getOrderList(final int Index) {
        CommonSubscriber<HttpResponse<List<OrderFatherListBean>>> subscriber = new CommonSubscriber<>(new SubscriberListener<HttpResponse<List<OrderFatherListBean>>>() {
            @Override
            public void onError(String e, int code) {
                fragment.shSwipeRefreshLayout.finishRefresh();
                fragment.shSwipeRefreshLayout.finishLoadmore();
                Toast.makeText(fragment.getActivity(), "获取订单列表失败", Toast.LENGTH_SHORT).show();
            }
            //分页
            @Override
            public void onNext(HttpResponse<List<OrderFatherListBean>> o) {
                fragment.shSwipeRefreshLayout.finishRefresh();
                fragment.shSwipeRefreshLayout.finishLoadmore();
                if (o.getCode() == COMMON_SUCCESS) {
                    List<OrderFatherListBean.FatherOrderBean> data = handleList(o.getData());
                    pageIndex = Index;
                    fragment.shSwipeRefreshLayout.setLoadmoreEnable(o.getHas_more() == 1);
                    if (Index == 1) {
                        if (adapter == null) {
                            if (o.getData() != null && o.getData().size() > 0) {
                                adapter = new CommonAdapter<>(fragment.getActivity(), data, R.layout.item_order_father, BR.itemFatherOrder, null);
                                adapter.setItemClick(new ItemClick() {
                                    @Override
                                    public void onItemClick(View view, int position) {
                                        //跳转订单详情
                                        Intent intent = new Intent(fragment.getActivity(), ChildOrderActivity.class);
                                        intent.putExtra("parent_order_id", adapter.getData().get(position).getId());
                                        fragment.startActivity(intent);
                                    }
                                });
                                fragment.orderList.setAdapter(adapter);
                            }
                        } else {
                            if (data == null || data.size() == 0) {
                                adapter.clear();
                            } else {
                                adapter.setData(data);
                            }
                            adapter.notifyDataSetChanged();
                        }
                    } else if (Index > 1) {
                        if (data != null && data.size() > 0) {
                            adapter.addData(data);
                            adapter.notifyItemRangeInserted(adapter.getData().size(), data.size());
                        }
                    }
                }
            }
        });
        RetrofitMethod.getInstance().getFatherOrderList(subscriber, SPUtil.getShareString(Constant.USER_ID), type, Index);
    }

    private List<OrderFatherListBean.FatherOrderBean> handleList(List<OrderFatherListBean> list) {
        List<OrderFatherListBean.FatherOrderBean> temp = new ArrayList<>();
        if (null == list || list.size() == 0) {
            return temp;
        }
        for (OrderFatherListBean orderFatherListBean : list) {
            temp.add(orderFatherListBean.getParent_order_info());
        }
        return temp;
    }
}